package com.library.control.login;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.library.model.dto.register.RegisterDTO;
import com.library.model.service.UserUpdateService;

public class RegiUpdateController extends SimpleFormController{

	private UserUpdateService userupdateservice;
	
	public void setUserupdateservice(UserUpdateService userupdateservice) {
		this.userupdateservice = userupdateservice;
	}

	public RegiUpdateController() {
		this.setCommandClass(RegisterDTO.class);
		this.setCommandName("regiupdate");
		this.setFormView("regiupdate");
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		//String member_id=request.getParameter("member_id");
		//String password=request.getParameter("password");
		//String name=request.getParameter("name");
		String jumin=request.getParameter("jumin1")+"-"+request.getParameter("jumin2");
		String phone=request.getParameter("phone1")+"-"+request.getParameter("phone2")+"-"+request.getParameter("phone3");
		//String email=request.getParameter("email");
		//String zipcode=request.getParameter("zipcode");
		//String address=request.getParameter("address");
		//String detailaddress=request.getParameter("detailaddress");
		RegisterDTO dtos=(RegisterDTO)super.formBackingObject(request);
		dtos.setJumin(jumin);
		dtos.setPhone(phone);
		//RegisterDTO dto=new RegisterDTO(member_id, password, name, jumin, phone, email, zipcode, address, detailaddress);
		
		return dtos;
	}
	
	protected ModelAndView onSubmit(Object command) throws Exception {
		RegisterDTO dto=(RegisterDTO)command;
		userupdateservice.updateUser(dto);
		ModelAndView mav=new ModelAndView();
		mav.setViewName("regiupdateok");
		return mav;
	}
	
	protected Map referenceData(HttpServletRequest request) throws Exception {
		String member_id=request.getParameter("member_id");
		RegisterDTO dto=userupdateservice.getUser(member_id);
		Map<String, RegisterDTO> map=new HashMap<String, RegisterDTO>();
		map.put("users", dto);
		return map;
	}
	
}
